//
//  HMLrcView.m
//  04-黑马音乐
//
//  Created by apple on 15-3-29.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "HMLrcView.h"
#import "HMLrcLine.h"
#import "HMLrcCell.h"
#import "UIView+AdjustFrame.h"

@interface HMLrcView() <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSArray *lrcArray;

@property (assign, nonatomic) NSInteger currentIndex;

@end

@implementation HMLrcView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupTableView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupTableView];
    }
    return self;
}

- (void)setupTableView
{
    // 添加控件
    UITableView *tableView = [[UITableView alloc] init];
    tableView.allowsSelection = NO;
    tableView.dataSource = self;
    tableView.delegate = self;
     [self addSubview:tableView];
    self.tableView = tableView;
    
}



- (void)layoutSubviews{
   [super layoutSubviews];
    self.tableView.frame = self.bounds;
    self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.height * 0.5, 0, self.tableView.height * 0.5, 0);
    // 有frame才有VIEW,此时设置数据才有效果
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor  = [UIColor clearColor];
}

#pragma mark - tableView的数据源和代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.lrcArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMLrcCell *lrcCell = [HMLrcCell lrcCellWithTableView:tableView];
    
    if (self.currentIndex == indexPath.row) {
        lrcCell.textLabel.font = [UIFont boldSystemFontOfSize:20.0];
    } else {
        lrcCell.textLabel.font = [UIFont systemFontOfSize:15.0];
    }
    
    HMLrcLine *lrcLine = self.lrcArray[indexPath.row];
    lrcCell.lrcLine= lrcLine;
    
    return lrcCell;
}

#pragma mark - 其他方法
/*!
 *  加载歌词
 *
 *  @param lrcName 歌曲词名字
 */
- (void)setLrcName:(NSString *)lrcName
{
    _lrcName = lrcName;
    
    // 1.加载歌词
    NSString *lrcFilePath = [[NSBundle mainBundle] pathForResource:lrcName ofType:nil];
    NSString *lrcString = [NSString stringWithContentsOfFile:lrcFilePath encoding:NSUTF8StringEncoding error:nil];
    
    // 2.利用换行符切割歌词
    NSArray *lrcArray = [lrcString componentsSeparatedByString:@"\n"];
    
    // 3.输入每一行歌词
    NSMutableArray *tempArray = [NSMutableArray array];
    for (NSString *lrc in lrcArray) {
        // 3.1.如果是不需要的内容,不需要放入数组中
        if (![lrc hasPrefix:@"["] || [lrc hasPrefix:@"[ti:"] || [lrc hasPrefix:@"[ar:"] || [lrc hasPrefix:@"[al:"]) {
            continue;
        }
        
        HMLrcLine *lrcLine = [[HMLrcLine alloc] init];
        
        NSArray *array = [lrc componentsSeparatedByString:@"]"];
        // [00:02:74
        lrcLine.time = [[array firstObject] substringFromIndex:1];
        lrcLine.text = [array lastObject];
        
        [tempArray addObject:lrcLine];
    }
    
    // 4.设置数据
    self.lrcArray = tempArray;
    
    // 5.刷新表哥
    [self.tableView reloadData];
}

- (void)setCurrentTime:(NSTimeInterval)currentTime
{
    if (currentTime < _currentTime) {
        self.currentIndex = -1;
    }
    
    _currentTime = currentTime;
    
    NSInteger min = currentTime / 60;
    NSInteger second = (NSInteger)currentTime % 60;
    NSInteger mSecond = (currentTime - (NSInteger)currentTime) * 100;
    NSString *currentTimeStr = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", min, second, mSecond];
    
    for (int i = (int)self.currentIndex + 1; i < self.lrcArray.count; i++) {
        HMLrcLine *line = self.lrcArray[i];
        NSString *lineTime = line.time;
        
        NSInteger nextIndex = i + 1;
        if (nextIndex < self.lrcArray.count) {
            HMLrcLine *nextLine = self.lrcArray[nextIndex];
            
            if ([currentTimeStr compare:lineTime] == NSOrderedDescending
                && [currentTimeStr compare:nextLine.time] == NSOrderedAscending && self.currentIndex != i) {
                
                // 刷新两行
                NSArray *indexPaths = @[
                        [NSIndexPath indexPathForRow:self.currentIndex inSection:0],
                        [NSIndexPath indexPathForRow:i inSection:0]
                                        ];
                
                self.currentIndex = i;
                
                [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
                [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
                
                break;
            }
        }
    }
}

@end
